/*
 * @Author: szx
 * @Date: 2021-12-25 13:17:54
 * @LastEditTime: 2021-12-25 13:40:59
 * @Description:
 * @FilePath: \leetcode\1600-1699\1609\1609.js
 */
/**
 * @param {TreeNode} root
 * @return {boolean}
 */
var isEvenOddTree = function (root) {
    const que = [root];
    let flag = 0;
    while (que.length) {
        const len = que.length;
        let pre = que.shift();
        if ((flag == 0 && pre.val % 2 == 0) || (flag == 1 && pre.val % 2 == 1)) return false;
        if (pre.left) que.push(pre.left);
        if (pre.right) que.push(pre.right);
        for (let i = 1; i < len; i++) {
            let node = que.shift();
            if (flag == 0 && (node.val % 2 == 0 || node.val <= pre.val)) return false;

            if (flag == 1 && (node.val % 2 == 1 || node.val >= pre.val)) return false;

            pre = node;
            if (node.left) que.push(node.left);
            if (node.right) que.push(node.right);
        }
        flag ^= 1;
    }
    return true;
};
